package com.yysjyr.logisticssystem.common.util;

import com.yysjyr.logisticssystem.workbench.entity.Order;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 吃货大人
 * @date: 2023/10/26
 * @time : 16:29
 * @description: 订单类的poi用户
 */

public class OrderPoiUtils {
    private OrderPoiUtils() {
    }


    public static List<Order> excelToOrderList(Workbook workbook) {
        List<Order> orderList = new ArrayList<>();
        Sheet sheet = workbook.getSheetAt(0); // Assuming the data is in the first sheet
        int rowCount = sheet.getLastRowNum();
        Row row;
        Cell cell;
        Order order = null;
        for (int i = 0; i <= rowCount; i++) {
            row = sheet.getRow(i);
            if (row == null || row.getCell(0) == null) {
                continue; // Skip empty rows or rows without an ID column
            }
            order = new Order();
            order.setId(row.getCell(0).getStringCellValue());
            order.setContent(row.getCell(1).getStringCellValue());
            order.setType(row.getCell(2).getStringCellValue());
            order.setCreateTime(row.getCell(3).getStringCellValue());
            order.setCreateBy(row.getCell(4).getStringCellValue());
            order.setEditFlag(row.getCell(5).getStringCellValue());
            order.setEditTime(row.getCell(6).getStringCellValue());
            order.setEditBy(row.getCell(7).getStringCellValue());
            order.setAddress(row.getCell(8).getStringCellValue());
            order.setState(row.getCell(9).getStringCellValue());
            order.setAttractionTime(row.getCell(10).getStringCellValue());
            order.setCourierId(row.getCell(11).getStringCellValue());
            orderList.add(order);
        }
        return orderList;
    }

    public static Workbook orderListToExcel(List<Order> orders){
        Workbook workbook = new XSSFWorkbook(); // 创建新的Excel工作簿
        Sheet sheet = workbook.createSheet("Order List"); // 创建新的工作表

        // 创建表头行
        Row headerRow = sheet.createRow(0);
        Cell headerCell = headerRow.createCell(0);
        headerCell.setCellValue("订单编号");
        headerCell = headerRow.createCell(1);
        headerCell.setCellValue("物品内容");
        headerCell = headerRow.createCell(2);
        headerCell.setCellValue("物品类型");
        headerCell = headerRow.createCell(3);
        headerCell.setCellValue("创建时间");
        headerCell = headerRow.createCell(4);
        headerCell.setCellValue("创建者");
        headerCell = headerRow.createCell(5);
        headerCell.setCellValue("是否修改");
        headerCell = headerRow.createCell(6);
        headerCell.setCellValue("修改时间");
        headerCell = headerRow.createCell(7);
        headerCell.setCellValue("修改者");
        headerCell = headerRow.createCell(8);
        headerCell.setCellValue("地址");
        headerCell = headerRow.createCell(9);
        headerCell.setCellValue("物流状态");
        headerCell = headerRow.createCell(10);
        headerCell.setCellValue("揽件时间");
        headerCell = headerRow.createCell(11);
        headerCell.setCellValue("揽件者");

        // 写入订单数据行
        int rowNum = 1;
        for (Order order : orders) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(order.getId());
            row.createCell(1).setCellValue(order.getContent());
            row.createCell(2).setCellValue(order.getType());
            row.createCell(3).setCellValue(order.getCreateTime());
            row.createCell(4).setCellValue(order.getCreateBy());
            row.createCell(5).setCellValue(order.getEditFlag());
            row.createCell(6).setCellValue(order.getEditTime());
            row.createCell(7).setCellValue(order.getEditBy());
            row.createCell(8).setCellValue(order.getAddress());
            row.createCell(9).setCellValue(order.getState());
            row.createCell(10).setCellValue(order.getAttractionTime());
            row.createCell(11).setCellValue(order.getCourierId());
        }
        return workbook;
    }

    public static boolean workbookToFile(String url,String fileName,Workbook workbook){
        try (FileOutputStream outputStream = new FileOutputStream(url+fileName+".xlsx")) { // 创建一个输出流，用于将数据写入Excel文件
            workbook.write(outputStream); // 将工作簿写入输出流中  \
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (workbook != null) {
                    workbook.close(); // 关闭工作簿对象，释放资源
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    public static boolean workbookToFile(Workbook workbook){
        return workbookToFile(PathUtils.getPath(),"Orders",workbook);
    }
}
